package com.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookDataMap implements DataMapInterface {
	public static String BOOK_ID = "book_id";
	public static String BOOK_ISBN = "book_isbn";
	public static String BOOK_NAME = "book_name";
	public static String BOOK_AUTHOR = "book_author";
	public static String BOOK_YEAR = "book_year";
	public static String BOOK_CALLNO = "book_callno";
	private Map<String, Map<String, String>> booksMap;
	private BookFileDataSet data;

	public BookDataMap() {
		Configer cf = new Configer();
		data = new BookFileDataSet(cf.getString("bookFilePath"));
		data.openDataSource();

		constuct();
	}

	private void constuct() {
		booksMap = new HashMap<String, Map<String, String>>();
		// remove the title line
		data.next();
		// begin
		while (data.haveNext()) {
			String line = (String) data.next();

			String[] attrs = line.split("\t");
			// remove those books without enough informations
			if (attrs.length <= 5||attrs[2]=="") {
				continue;
			}
			Map<String, String> book = new HashMap<String, String>();
			book.put(BOOK_ISBN, attrs[1]);
			book.put(BOOK_NAME, attrs[2]);
			book.put(BOOK_AUTHOR, attrs[3]);
			book.put(BOOK_YEAR, attrs[4]);
			book.put(BOOK_CALLNO, attrs[5]);
			// put new book into map
			booksMap.put(attrs[0], book);
			
		}
		System.out.println("done");

	}

	public Object getCluster(String columns, String value) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getValue(String key, String colums) {
		// TODO Auto-generated method stub
		if (booksMap.containsKey(key)) {
			Map<String, String> book = booksMap.get(key);
			return book.get(colums);
		} else
			return null;
	}

}
